package com.demo.systemmonitor;

import android.app.Activity;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import com.demo.systemmonitor.R;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

public class TimeRangeSelectorActivity extends Activity {
    private static final String TAG = "TimeRangeSelector";
    
    private TextView tvStartTime, tvEndTime;
    private Button btnSelectStartTime, btnSelectEndTime, btnConfirm, btnCancel;
    
    private Calendar startTime, endTime;
    private SimpleDateFormat timeFormat;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_time_range_selector);
        
        initViews();
        initTime();
    }
    
    private void initViews() {
        tvStartTime = findViewById(R.id.tv_start_time);
        tvEndTime = findViewById(R.id.tv_end_time);
        btnSelectStartTime = findViewById(R.id.btn_select_start_time);
        btnSelectEndTime = findViewById(R.id.btn_select_end_time);
        btnConfirm = findViewById(R.id.btn_confirm);
        btnCancel = findViewById(R.id.btn_cancel);
        
        btnSelectStartTime.setOnClickListener(v -> showStartTimePicker());
        btnSelectEndTime.setOnClickListener(v -> showEndTimePicker());
        btnConfirm.setOnClickListener(v -> confirmSelection());
        btnCancel.setOnClickListener(v -> finish());
    }
    
    private void initTime() {
        timeFormat = new SimpleDateFormat("HH:mm", Locale.getDefault());
        startTime = Calendar.getInstance();
        endTime = Calendar.getInstance();
        
        // 修改：默认设置为过去1小时，而不是未来1小时
        startTime.add(Calendar.HOUR, -1); // 开始时间：1小时前
        // endTime 保持当前时间
        
        updateTimeDisplay();
    }
    
    private void showStartTimePicker() {
        TimePickerDialog dialog = new TimePickerDialog(this, 
            new TimePickerDialog.OnTimeSetListener() {
                @Override
                public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                    startTime.set(Calendar.HOUR_OF_DAY, hourOfDay);
                    startTime.set(Calendar.MINUTE, minute);
                    updateTimeDisplay();
                }
            },
            startTime.get(Calendar.HOUR_OF_DAY),
            startTime.get(Calendar.MINUTE),
            true
        );
        dialog.show();
    }
    
    private void showEndTimePicker() {
        TimePickerDialog dialog = new TimePickerDialog(this, 
            new TimePickerDialog.OnTimeSetListener() {
                @Override
                public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                    endTime.set(Calendar.HOUR_OF_DAY, hourOfDay);
                    endTime.set(Calendar.MINUTE, minute);
                    updateTimeDisplay();
                }
            },
            endTime.get(Calendar.HOUR_OF_DAY),
            endTime.get(Calendar.MINUTE),
            true
        );
        dialog.show();
    }
    
    private void updateTimeDisplay() {
        tvStartTime.setText(timeFormat.format(startTime.getTime()));
        tvEndTime.setText(timeFormat.format(endTime.getTime()));
    }
    
    private void confirmSelection() {
        if (endTime.before(startTime)) {
            Toast.makeText(this, "结束时间不能早于开始时间", Toast.LENGTH_SHORT).show();
            return;
        }
        
        // 返回选择的时间范围
        getIntent().putExtra("start_time", timeFormat.format(startTime.getTime()));
        getIntent().putExtra("end_time", timeFormat.format(endTime.getTime()));
        setResult(RESULT_OK, getIntent());
        finish();
    }
}
